package com.beancore.util;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * This class is used to load the picture of each item from a whole picture
 */
public class ImageLoader {

    private BufferedImage sourceImg;

    public ImageLoader(String imagePath) throws IOException {
        sourceImg = ImageIO.read(new File(getClass().getResource("/").getPath() + "/" + imagePath));
    }

    public Image getImage(int posX, int posY, int width, int height) {
        BufferedImage targetImg = this.sourceImg.getSubimage(posX, posY, width, height);
        Image img = new ImageIcon(targetImg).getImage();
        return img;
    }

}
